#!/usr/bin/env python
import wx
import sys
import os
class Debug(wx.Frame):
    	def __init__(par, parent, title):
		pass


class Main(wx.Frame):
	filename = ""
	dirname = ""
	window = None
	plugin = ""
	""" We simply derive a new class of Frame. """
    	def __init__(self, parent, title):
		self.window = wx.Frame.__init__(self, parent, title=title, size=(800,600))
        	self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,size=(600,400))
		self.Bind(wx.EVT_TEXT, self.OnText, self.control)
	
		filemenu = wx.Menu()
		new = filemenu.Append(wx.ID_NEW, "&New", "Create a new file")
		openfile = filemenu.Append(wx.ID_OPEN, "&Open", "Open an existing file")
		save = filemenu.Append(wx.ID_SAVE, "&Save", "Save the current file")
		saveas = filemenu.Append(wx.ID_SAVEAS, "Save &As", "Save current file under a specific name")
		source = filemenu.Append(wx.ID_ANY, "S&how Source" , "Show the source of this programm")

		filemenu.AppendSeparator()
		about = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")

		filemenu.AppendSeparator()
		exit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")


		self.Bind(wx.EVT_MENU, self.OnNew, new)
		self.Bind(wx.EVT_MENU, self.OnOpen, openfile)
		self.Bind(wx.EVT_MENU, self.OnSave, save)
		self.Bind(wx.EVT_MENU, self.OnSaveAs, saveas)
		self.Bind(wx.EVT_MENU, self.OnSource, source)

		self.Bind(wx.EVT_MENU, self.OnAbout, about)
		self.Bind(wx.EVT_MENU, self.OnExit, exit)






		editmenu = wx.Menu()
		LoadPlugin = editmenu.Append(wx.ID_ANY, "load &Plugin","load a plugin")

		self.Bind(wx.EVT_MENU, self.onLoadPlugin,LoadPlugin)

		menubar = wx.MenuBar()
		menubar.Append(filemenu,"&File")
		menubar.Append(editmenu,"&Edit")
		self.SetMenuBar(menubar)
		self.Show(True)
	def OnText(self,event):
		if self.plugin:
			print self.plugin
			execfile(self.plugin)
	def OnNew(self,event):
		self.filename = ""
		self.dirname = ""
		self.control.SetValue("")
	def OnOpen(self,e):
        	""" Open a file"""
        	self.dirname = ''
        	dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			f = open(os.path.join(self.dirname, self.filename), 'r')
			self.control.SetValue(f.read())
			f.close()
	        dlg.Destroy()
	def OnSave(self,event):
		if (self.filename != "") and (self.dirname != ""):
			f = open(os.path.join(self.dirname,self.filename),"w")
			f.write(self.control.GetValue())
			f.close()
		else:
			self.OnSaveAs(event)
	def OnSaveAs(self,event):
		dlg = wx.FileDialog(self, "Save as...", self.dirname, "~/", "*", wx.FD_SAVE)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			self.OnSave(event)
	def OnSource(self,event):
		pass
	def OnAbout(self,event):
		dlg = wx.MessageDialog( self, "A small text editor", "About Sample Editor", wx.OK)
		dlg.ShowModal()
		dlg.Destroy()
	def OnExit(self,event):
		sys.exit(1)

	def OnDebug(self,event):
		#debug = Debug(self,"Debug")
		wx.Frame.__init__(self,self.window,title="Debug")
	def onLoadPlugin(self,event):
		dlg = wx.FileDialog(self, "Choose a file", self.dirname, "./plugins/", "*.*", wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.plugin = dlg.GetDirectory()
			self.plugin = os.path.join(self.plugin, dlg.GetFilename())
	        dlg.Destroy()




app = wx.App(False)
frame = Main(None, 'Small editor')
app.MainLoop()

